package com.ayf.payment.game.api.controller;

import com.ayf.payment.game.api.SiteStatisticsRemoteService;
import com.ayf.payment.game.api.dto.MerchantDTO;
import com.ayf.payment.game.api.dto.SiteStatisticsDTO;
import com.ayf.payment.game.api.dto.SiteStatisticsSummaryDTO;
import com.ayf.payment.game.api.dto.criteria.SiteStatisticsCriteria;
import com.ayf.payment.game.api.exception.BusinessException;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.utils.CommonResult;
import com.ayf.payment.game.api.utils.DateUtils;
import com.ayf.payment.game.api.utils.IPUtils;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RequestMapping("/v/site/statistics")
@RestController
public class SiteStatisticsController {

    @Reference(group = "xygpay-rpc-statistics",version = "${XY_STATISTICS_DUBBO_VERSION}")
    private SiteStatisticsRemoteService siteStatisticsRemoteService;

    /**
     * 流量统计接口
     * @param request
     * @return
     */
    @GetMapping("/flow")
    public CommonResult<Boolean> flow(HttpServletRequest request) {
        String m = request.getParameter("m");
        if (StringUtils.isEmpty(m)) {
            throw new ParameterException("流量统计失败，商户ID不允许为空");
        }
        Integer merchantId = Integer.valueOf(m);
        String siteUrl = request.getParameter("s");
        if (StringUtils.isEmpty(siteUrl)) {
            LogPortal.error("商户ID:{} 流量统计失败，网站地址不允许为空",merchantId);
            throw new ParameterException("流量统计失败，网站地址不允许为空");
        }
        try {
            String playerIp = IPUtils.getIp(request);
            siteUrl = getSiteUrl(siteUrl);
            LogPortal.info("商户ID:{}，发布站URL:{}，玩家IP:{}",merchantId,siteUrl,playerIp);
            SiteStatisticsDTO siteStatisticsDTO = new SiteStatisticsDTO();
            siteStatisticsDTO.setMerchantId(merchantId);
            siteStatisticsDTO.setSiteUrl(siteUrl);
            Date currentDate = DateUtils.getNowDate();
            siteStatisticsDTO.setGenerateTimeIndex(DateUtils.dateToInteger(currentDate));
            siteStatisticsDTO.setCreateTime(currentDate);
            siteStatisticsDTO.setPlayerIp(playerIp);
            siteStatisticsRemoteService.saveSiteStatistics(siteStatisticsDTO);
        } catch (Exception e) {
            LogPortal.error("商户ID:{} 流量统计失败，发生了{}异常，异常描述:{}",e,merchantId,e.getClass().getName(),e.getMessage());
            return new CommonResult<Boolean>(CommonConstants.RETURN_ERROR, "流量统计失败，失败描述:"+e.getMessage(),false);
        }
        return new CommonResult<Boolean>(CommonConstants.RETURN_SUCCESS, "流量统计成功", true);
    }

    /**
     * 获取网站域名信息
     * @param siteUrl
     * @return
     */
    private String getSiteUrl(String siteUrl) {
        String [] siteUrlArry=siteUrl.split("//");
        if (siteUrlArry.length>1){
            String [] siteUrlArry2=siteUrlArry[1].split("/");
            siteUrl = siteUrlArry2[0];
        }
        return siteUrl;
    }

    /**
     * 游戏人数统计接口
     * @param request
     * @return
     */
    @GetMapping("/players")
    public CommonResult<Boolean> players(HttpServletRequest request) {
        String m = request.getParameter("m");
        if (StringUtils.isEmpty(m)) {
            throw new ParameterException("游戏人数统计失败，商户ID不允许为空");
        }
        Integer merchantId = Integer.valueOf(m);
        try {
            String playerIp = IPUtils.getIp(request);
            LogPortal.info("游戏人数统计 商户ID:{} 玩家IP:{}",merchantId,playerIp);
            SiteStatisticsDTO siteStatisticsDTO = new SiteStatisticsDTO();
            siteStatisticsDTO.setMerchantId(merchantId);
            siteStatisticsDTO.setPlayerIp(playerIp);
            Date currentDate = DateUtils.getNowDate();
            siteStatisticsDTO.setGenerateTimeIndex(DateUtils.dateToInteger(currentDate));
            siteStatisticsDTO.setCreateTime(currentDate);
            siteStatisticsRemoteService.saveSiteStatisticsPlayers(siteStatisticsDTO);
        } catch (Exception e) {
            LogPortal.error("商户ID:{} 游戏人数统计失败，发生了{}异常，异常描述:{}",e,merchantId,e.getClass().getName(),e.getMessage());
            return new CommonResult<Boolean>(CommonConstants.RETURN_ERROR, "游戏人数统计失败，失败描述:"+e.getMessage(),false);
        }
        return new CommonResult<Boolean>(CommonConstants.RETURN_SUCCESS, "游戏人数统计成功", true);
    }

    /**
     * 站点统计查询列表
     * @param merchant
     * @param criteria
     * @return
     */
    @PostMapping("/list")
    public CommonResult<SiteStatisticsSummaryDTO> list(MerchantDTO merchant, @RequestBody SiteStatisticsCriteria criteria) {
        if (merchant == null) {
            throw new BusinessException("请登录");
        }
        criteria.setMerchantId(merchant.getId());
        SiteStatisticsSummaryDTO siteStatisticsSummaryDTO = siteStatisticsRemoteService.list(criteria);
        return new CommonResult<SiteStatisticsSummaryDTO>(CommonConstants.RETURN_SUCCESS, "站点统计列表查询成功",siteStatisticsSummaryDTO);
    }
}
